package com.virgo.demo.support.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.demo.support.converter.AreaConverter;
import com.virgo.demo.support.domain.AreaDo;
import com.virgo.demo.support.domain.vo.AreaVo;
import com.virgo.demo.support.mapper.AreaMapper;
import com.virgo.demo.support.service.AreaService;
import com.virgo.redis.annotation.ICache;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaDo> implements AreaService {

	private final AreaMapper areaMapper;

	private final AreaConverter areaConverter;

	/**
	 * 获取地区列表
	 * @param parentId Long
	 * @return List<AreaVo>
	 */
	@Override
	@ICache(keyPrefix = "area_list", matchValue = "#parentId", ttlMin = 86400, ttlMax = 604800)
	public List<AreaVo> findList(Long parentId) {
		LambdaQueryWrapper<AreaDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AreaDo::getParentId,parentId);
		return areaConverter.convert(areaMapper.selectList(queryWrapper),AreaVo.class);
	}

	/**
	 * 地区详情
	 * @param id Long
	 * @return AreaVo
	 */
	@Override
	@ICache(keyPrefix = "area_info", matchValue = "#id", ttlMin = 86400, ttlMax = 172800)
	public AreaVo findInfo(Long id) {
		return areaConverter.convert(getById(id),AreaVo.class);
	}
}